<?php
/**
 * @author: huhaiqin
 * @Date: 2023-04-03
 * @desc: 自定义页面
 */
namespace Game\Model;
use Think\Model;

class PageModel extends Model{

    /**
     * 添加、编辑
     */
    public function _edit($post = []){
        // $post['data'] = html_entity_decode($post['data']);
        // $post['data'] = json_decode($post['data']);
        // $post['data'] = json_encode($post['data'],JSON_UNESCAPED_UNICODE);
        $post['update_time'] = time();
        // $data = $this->create($post);
        $data = $post;
        if($data['id']){
            //编辑
            $rdata = $this->save($data);
            if($rdata){
                _apiReturn(1,'success', $rdata);
            }else{
                _apiReturn(0,'error');
            }
        }else{
            //添加
            $data['create_time'] = $data['update_time'];
            $rdata = $this->add($data);
            if($rdata){
                _apiReturn(1,'success', $rdata);
            }else{
                _apiReturn(0,'error');
            }
        }
    }

    /**
     * 软删除
     */
    public function _del_ruan($id = 0){
        if(!$id){
            _apiReturn(0,'缺少参数');
        }
        $data = [
            'id'=>$id,
            'is_delete' => 1
        ];
        if($this->save($data)){
            _apiReturn(1,'success');
        }else{
            _apiReturn(0,'error');
        }
    }

    /**
     * 物理删除
     */
    public function _del($ids = 0){
        if(count($ids) == 0){
            _apiReturn(0,'ID不能为空~');
        }
        $map['id'] = ['in',$ids];
        if($this->where($map)->delete()){
            _apiReturn(1,'success');
        }else{
            _apiReturn(0,'error');
        }
    }

    /**
     * 数据接口
     */
    public function _query($p = 1, $row = 10, $query = ''){
        $sql = 'SELECT A.* FROM mini_page A WHERE 1=1 ';
        $sqlcount = 'SELECT COUNT(A.`id`) AS tp_count FROM mini_page A WHERE 1=1 ';
        if ($query) {
            $sql .= $query;
            $sqlcount .= $query;
        }
        $sql .= ' ORDER BY A.listorder desc ';
        $sql .= ' LIMIT '.($p - 1) * $row.','.$row;
        $total = $this->query($sqlcount);
        $data['data'] = $this->query($sql);
        $data['page']['total'] = $total[0]['tp_count'];
        $data['page']['cur'] = $p;
        $data['page']['row'] = $row;
        // $data['sql'] = $sql;
        return $data;
    }
    
    /**
     * 详细数据
     */
    public function _find($id=0){
        if(!$id){
            _apiReturn(0,'缺少参数');
        }
        $data = $this->find($id);
        if(!$data){
            _apiReturn(0,'页面配置不存在');
        }
        $data['data'] = json_decode($data['data'],true);
        // _apiReturn(1,'success',$data);
        return $data;
    }

    /**
     * 根据Key获取详细数据
     */
    public function _find_by_key($key=''){
        if($key == ''){
            _apiReturn(0,'缺少参数');
        }
        $data = $this->where(['key'=>$key])->find();
        if(!$data){
            _apiReturn(0,'页面配置不存在');
        }
        $data['data'] = json_decode($data['data'],true);
        // _apiReturn(1,'success',$data);
        return $data;
    }

    /**
     * 2023.04.06  xuqixiang
     * 根据type获取数据
     */
    public function _find_by_type($type=''){
        if($type == ''){
            _apiReturn(0,'缺少参数');
        }
        $data = $this->where(['type'=>$type])->select();
        if(!$data){
            _apiReturn(0,'页面不存在');
        }
        return $data;
    }
}